package com.cdtu.store.item.controller;

import com.cdtu.store.common.entity.SpecGroupEntity;
import com.cdtu.store.common.entity.SpecParamEntity;
import com.cdtu.store.item.service.SpecGroupService;
import com.cdtu.store.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dongyuhao
 */
@RequestMapping("spec")
@RestController
public class SpecificationController {
    @Autowired
    private SpecParamService specParamService;
    @Autowired
    private SpecGroupService specGroupService;
    @GetMapping("params")
    public List<SpecParamEntity> queryParams(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "generic", required = false) Integer generic,
            @RequestParam(value = "searching", required = false) Integer searching
    ){
        List<SpecParamEntity> params = this.specParamService.queryParams(gid, cid, generic, searching);
        if (CollectionUtils.isEmpty(params)){
            return null;
        }
        return params;
    }

    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroupEntity>> querySpecsByCid(@PathVariable("cid") Long cid){
        List<SpecGroupEntity> list = this.specGroupService.querySpecsByCid(cid);
        if(list == null || list.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }
}
